/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SelectReggresionDialog.java
 *
 * Created on 2011-09-10, 15:07:20
 */
package JFreeChartExtensions.ChartPanelDialogs;

import JFreeChartExtensions.Collections.DatasetCollection;
import JFreeChartExtensions.ExtendableChartPanel;
import JFreeChartExtensions.Fitters.ChartFit;
import JFreeChartExtensions.Fitters.GaussianReggresion;
import JFreeChartExtensions.Fitters.HarmonicReggresion;
import JFreeChartExtensions.Fitters.IReggression;
import JFreeChartExtensions.Fitters.LinearReggression;
import JFreeChartExtensions.Fitters.PolynomialReggresion;
import JFreeChartExtensions.Series.XYExtendedIntervalSeries;
import java.util.Hashtable;
import java.util.Map;
import javax.swing.DefaultComboBoxModel;
import org.apache.commons.math.optimization.OptimizationException;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYIntervalSeries;

/**
 *
 * @author Nobody
 */
public class SelectReggresionDialog extends ChartPanelDialog {

    Map<String, IReggression> reggresionMap = new Hashtable<String, IReggression>();
    XYPlot xYPlot;
    ChartFit chartFit;

    /** Creates new form SetScaleDialog */
    public SelectReggresionDialog(ExtendableChartPanel chartPanel, double x, double y) {
        super(chartPanel);
        initComponents();
        reggresionMap.put("Linear Reggression", new LinearReggression());
        reggresionMap.put("Polynomial Reggression (2th degree) ", new PolynomialReggresion(2));
        reggresionMap.put("Polynomial Reggression (3th degree) ", new PolynomialReggresion(3));
        reggresionMap.put("Polynomial Reggression (4th degree) ", new PolynomialReggresion(4));
        reggresionMap.put("Harmonic Reggression ", new HarmonicReggresion());
        reggresionMap.put("Gaussian Reggression ", new GaussianReggresion());

        xYPlot = parentPanel.getXYPlot();
        this.chartFit = new ChartFit(x, y);
        fillComboBoxesWithValues();
    }

    private void fillComboBoxesWithValues() {
        fillRegressionComboBoxWithValues();
        fillSeriesComboBoxWithValues();
    }

    private void fillRegressionComboBoxWithValues() {
        DefaultComboBoxModel model = (DefaultComboBoxModel) seriesComboBox.getModel();
        for (XYIntervalSeries entry : parentPanel.getDatasetCollection().getAllNonRegressionSeries()) {
            model.addElement((String) (entry.getKey()));
        }
    }

    private void fillSeriesComboBoxWithValues() {
        DefaultComboBoxModel comboBoxModel = (DefaultComboBoxModel) regressionComboBoxModel.getModel();
        for (String mapKeyItem : reggresionMap.keySet()) {
            comboBoxModel.addElement(mapKeyItem);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        OkCancelPanel = new javax.swing.JPanel();
        OkButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        SeriesPanel = new javax.swing.JPanel();
        seriesComboBox = new javax.swing.JComboBox();
        RegressionPanel = new javax.swing.JPanel();
        regressionComboBoxModel = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        OkButton.setText("Ok");
        OkButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                OkButtonMouseClicked(evt);
            }
        });
        OkCancelPanel.add(OkButton);

        CancelButton.setText("Cancel");
        CancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CancelButtonMouseClicked(evt);
            }
        });
        OkCancelPanel.add(CancelButton);

        SeriesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Series"));

        javax.swing.GroupLayout SeriesPanelLayout = new javax.swing.GroupLayout(SeriesPanel);
        SeriesPanel.setLayout(SeriesPanelLayout);
        SeriesPanelLayout.setHorizontalGroup(
            SeriesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SeriesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(seriesComboBox, 0, 205, Short.MAX_VALUE)
                .addContainerGap())
        );
        SeriesPanelLayout.setVerticalGroup(
            SeriesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SeriesPanelLayout.createSequentialGroup()
                .addComponent(seriesComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        RegressionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Regression Type"));

        javax.swing.GroupLayout RegressionPanelLayout = new javax.swing.GroupLayout(RegressionPanel);
        RegressionPanel.setLayout(RegressionPanelLayout);
        RegressionPanelLayout.setHorizontalGroup(
            RegressionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, RegressionPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(regressionComboBoxModel, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        RegressionPanelLayout.setVerticalGroup(
            RegressionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(RegressionPanelLayout.createSequentialGroup()
                .addComponent(regressionComboBoxModel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(OkCancelPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(SeriesPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(RegressionPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(RegressionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(SeriesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(OkCancelPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CancelButtonMouseClicked
        dispose();
    }//GEN-LAST:event_CancelButtonMouseClicked

    private void OkButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_OkButtonMouseClicked
        String selectedItem = (String) regressionComboBoxModel.getModel().getSelectedItem();
        try {
            DatasetCollection collection = parentPanel.getDatasetCollection();
            XYIntervalSeries seriesToRegress = collection.getSeriesByName((String) seriesComboBox.getSelectedItem());
            XYExtendedIntervalSeries series = chartFit.regress((XYExtendedIntervalSeries) seriesToRegress, reggresionMap.get(selectedItem));
            collection.addRegressionSeries(seriesToRegress, series);
            /*    parent.getRenderCollection().addRendererForSeries(pair);*/
            dispose();
        } catch (OptimizationException ex) {
           // Main.getInstance().showErrorPopup(ex.getMessage());
        }
    }//GEN-LAST:event_OkButtonMouseClicked

   /* @Override
    public void dispose() {
        frameWithChart.setEnabled(true);
        super.dispose();
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JButton OkButton;
    private javax.swing.JPanel OkCancelPanel;
    private javax.swing.JPanel RegressionPanel;
    private javax.swing.JPanel SeriesPanel;
    private javax.swing.JComboBox regressionComboBoxModel;
    private javax.swing.JComboBox seriesComboBox;
    // End of variables declaration//GEN-END:variables
}
